<template>
	<div class="posa">
		<scrolls>			
			<car-options :carsList="dataLoading" ></car-options>
		</scrolls>
	</div>
</template>
<script>
	import scrolls from './scroll.vue'
	export default{
		data(){
			return {
				imgUrl:imgUrl,
				datas:[],
				thisParams:{
					params:{
						startDate:'',
						endDate:'',
						statisType:1,
					}
				},
				isScroll:false,
				dataLoading:[],
				params:{
					params:{}
				}
			}
		},
		components:{
			scrolls
		},
		
		mounted:function(){	
			this.queryRouter()
			this.dateInit()
			this.dataGetJson()
		},
		methods:{
			queryRouter() {
				//				console.log(this.$route.query)
				if(this.$route.query.time) {
					this.params.params.month = this.$route.query.time
					this.params.params.startDate = ''
				}
				if(this.$route.query.timeDay) {
					this.params.params.startDate = this.$route.query.timeDay
					this.params.params.month = ''
				}
				this.params.params.shopId = this.$route.query.shopId
				this.params.params.marketId = this.$route.query.marketId
				this.params.params.statisType = this.$route.query.typeId
				console.log(this.params)
			},
			// 请求数据初始化（下滑加载）
			dataInit(){
				this.dataLoading = []
				var b = (this.datas.length-this.dataLoading.length)>9?10:this.datas.length-this.dataLoading.length				
				for(var i = 0;i<b;i++)
					this.dataLoading.push(this.datas[i])
				if(this.datas.length == this.dataLoading.length){
					this.$store.commit('infiniteInitChange',2)
				}else{
					this.$store.commit('infiniteInitChange',0)
				}
			},
			// 下滑加载数据
			dataScrollPush(){
				if(this.dataLoading.length<this.datas.length){	
					setTimeout(()=>{
						var a = this.dataLoading.length-1
						var b = (this.datas.length-this.dataLoading.length)>9?this.dataLoading.length+9:this.datas.length
						console.log(b)
						for(var i = a;i<b;i++){
							this.dataLoading.push(this.datas[i])
						}
						this.$store.commit('infiniteInitChange',0)
					},500)
				}else{
					this.$store.commit('infiniteInitChange',2)
				}
			},
			// 请求数据
			dataGetJson(){
				window.axios.get(baseUrl+'/controller/getCarStatisList',this.thisParams).then((res)=>{					
					this.datas = res.data.result
					this.dataInit()
					this.undefinedImg()
					this.$store.commit('refreshInitChange',false)
				})
			},
			// 日期处理
			dateInit(){
				var _month = this.params.params.month
				var _startDate = this.params.params.startDate
				if(_month){
					this.thisParams.params.startDate = this.dateChange(_month)
					this.thisParams.params.endDate = this.dateChangeTo(_month)
				}
				if(_startDate){
					this.thisParams.params.startDate = _startDate
					this.thisParams.params.endDate = new Date((Date.parse(this.params.params.startDate)+1000*60*60*24))
				}
				this.thisParams.params.shopId = this.$route.query.shopId
				this.thisParams.params.marketId = this.$route.query.marketId
				this.thisParams.params.statisType = this.$route.query.typeId
			},
			// 日期处理
			dateChange(e){
				return e+'-01'
			},
			// 日期处理
			dateChangeTo(e){
				var time_y = parseInt(e.slice(5,7))
				var time_n = parseInt(e.slice(0,4))
				time_y++
				time_n++
				if((time_y)>12){
					return time_n.toString()+'-01-01'
				}else{
					if(time_y<10)
						time_y = '0'+time_y
					return e.slice(0,4)+'-'+time_y+'-01'
				}
			},
			
			//undefined图片处理
			undefinedImg(){
				for(let item of this.datas){
					if(item.carImageUrl == undefined){
						item.carImageUrl = ''
					}
				}				
			},
			
		},
		activated:function(){	
			// 菜单底线归位
			this.$emit('tabMenuChange',1)			
		},
		watch:{
			// 刷新中执行的函数
			'$store.state.scroll.refreshInit':function(){				
				if(this.$store.state.scroll.refreshInit){
					//重新发送请求
					this.dataGetJson()
				}
			},
			'$store.state.scroll.infiniteInit':function(){
				if(this.$store.state.scroll.infiniteInit==1){
					this.dataScrollPush()
				}
			}
		}
	}
</script>
